﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.LY;
using DFGK.IService.LY;
using DFGK_WMS.Model.Models.LyModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service.LY
{
    public class ScrapLossOrderService : BaseService, IScrapLossOrderService
    {
        private readonly IScrapLossOrderRepository _scrapLossOrderRepository;
        private readonly IUnitOfWork unitOfWork;
        private readonly IMapper mapper1;
        public ScrapLossOrderService(IMapper mapper, IScrapLossOrderRepository scrapLossOrderRepository, IUnitOfWork unitOfWork, IMapper mapper1) : base(mapper)
        {
            _scrapLossOrderRepository = scrapLossOrderRepository;
            this.unitOfWork = unitOfWork;
            this.mapper1 = mapper1;
        }

        public bool AddOrModefiyScrapLossOrder(ScrapLossOrder model, out string errorMsg)
        {
            errorMsg = string.Empty;
            if (model.Id > 0)
            {
                _scrapLossOrderRepository.UpdEntity(model);
            }
            else
            {
                _scrapLossOrderRepository.AddEntity(model);

            }
            return unitOfWork.SaveChangesEntity();
        }

        public bool DelScrapLossOrder(long scrapLossOrderId)
        {
            var Quote = _scrapLossOrderRepository.QueryEntityById(scrapLossOrderId);
            if (Quote == null)
            {
                return false;
            }
            _scrapLossOrderRepository.DelEntity(Quote);
            return unitOfWork.SaveChangesEntity();
        }

        public ScrapLossOrder? GetScrapLossOrder(long scrapLossOrderId)
        {
            return _scrapLossOrderRepository.QueryEntityById(scrapLossOrderId);
        }

        public List<ScrapLossOrder> GetScrapLossOrderData(string? name, out int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            var list = _scrapLossOrderRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(d => d.OperatorName.Contains(name));
            }
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(d => d.Id);
            return list.ToList();
        }
        public List<ScrapLossOrder> GetScrapLossOrderExcel()
        {
            

            var list = _scrapLossOrderRepository.GetEntityAlls();
            
            return list.ToList();

        }
    }
}
